import Vue from 'vue'
import VueRouter from "vue-router"
Vue.use(VueRouter)

//导入网页进度条
import NProgress from "../../node_modules/nprogress/nprogress.js"
import "../../node_modules/nprogress/nprogress.css"

import Home from "@/views/Home.vue"
import Users from "@/views/Users.vue"
import Myress from "@/views/Myress.vue"
import addMyress from "@/views/addMyress.vue"
import editMyress from "@/views/editMyress.vue"
import Shop from "@/views/Shop.vue"
import GoodsDetail from "@/views/GoodsDetail.vue"
import Goodslist from "@/views/Goodslist.vue"
import VantScroll from "@/views/VantScroll.vue"
import Seek from "@/views/Seek.vue"
import SeekResult from "@/views/SeekResult.vue"
import store from '@/store/index.js'
import orderList from "@/views/orderList.vue"
import orderDetail from "@/views/orderDetail.vue"

// import NewList from "@/views/NewList.vue"

// 创建路由器对象
const router = new VueRouter({
  routes: [{
    path: "/",
    redirect: "/home"
  },
  {
    path: "/home",
    name: 'Home',
    component: Home,
    meta: {
      homeTitle: "首页",
      homePage: true,
      isShowNavBar: false,
    },
    // 子路由匹配的内容要展示在父组件中的router-view中
    children: [{
      path: "seek",
      component: Seek,
      name: "seek",
      meta: {
        homeTitle: "搜索",
        isShowNavBar: false,
      },
    }]
  },
  {
    path: "/seek-result/:keyword",
    component: () => import('@/views/SeekResult.vue'),
    name: "SeekResult",
    meta: {
      homeTitle: "搜索结果",
      isShowNavBar: false,
    },
  },
  {
    path: "/Shop",
    component: Shop,
    name: "Shop",
    meta: {
      homeTitle: "购物车",
      homePage: true,
      isShowNavBar: true,
      requireAuth: true
    }
  },
  {
    path: "/users",
    component: Users,
    name: "Users",
    meta: {
      homeTitle: "个人中心",
      homePage: true,
      isShowNavBar: true,
      requireAuth: true
    }
  },
  {
    path: "/Myress",
    component: Myress,
    meta: {
      homeTitle: "我的地址",
      homePage: false,
      isShowNavBar: true,
      requireAuth: true
    },
  },

  {
    path: "/addMyress",
    component: addMyress,
    meta: {
      homeTitle: "添加地址",
      isShowNavBar: true
    }
  },
  {
    path: "/editMyress/:addressInfo",
    props: true,
    component: editMyress,
    meta: {
      homeTitle: "编辑地址",
      isShowNavBar: true
    }
  },
  {
    path: "/goodsDetail/:id",
    component: GoodsDetail,
    props: true,
    meta: {
      homeTitle: "商品详情",
      isShowNavBar: true
    }
  },
  {
    path: "/goodslist",
    component: Goodslist,
    meta: {
      homeTitle: "商品列表",
      isShowNavBar: true,
      requireAuth: true
    }
  },
  {
    path: "/vantscroll",
    component: VantScroll,
    meta: {
      homeTitle: "vant-scroll",
      isShowNavBar: true
    }
  },
  {
    path: "/newlist",
    component: () => import('@/views/NewList.vue'),
    meta: {
      homeTitle: "新闻列表",
      isShowNavBar: true
    }
  },
  {
    path: "/NewDetail/:id",
    component: () => import('@/views/NewDetail.vue'),
    props: true,
    meta: {
      homeTitle: "新闻详情",
      isShowNavBar: true
    }
  },
  {
    path: "/Login",
    component: () => import('@/views/Login.vue'),
    props: true,
    meta: {
      homeTitle: "登录",
      isShowNavBar: true
    }
  },
  {
    path: "/Register",
    component: () => import('@/views/Register.vue'),
    props: true,
    meta: {
      homeTitle: "注册",
      isShowNavBar: true
    }
  },
  {
    path: "/Seet",
    component: () => import('@/components/Seet.vue'),
    props: true,
    meta: {
      // homeTitle: "注册",
      // isShowNavBar: true
    }
  },
  {
    path: "/orderList",
    component: orderList,
    meta: {
      homeTitle: "我的订单",
      isShowNavBar: true,
      requireAuth: true
    }
  },
  {
    path:"/orderDetail/:order_id",
    component:orderDetail,
    props:true,
    meta:{
      homeTitle:'订单详情',
      isShowNavBar:true,
      requireAuth:true
    }
  },
  ]
})

NProgress.configure({ showSpinner: false });
//前守卫
router.beforeEach((to, from, next) => {
  NProgress.start();
  console.log('requireAuth:', to.meta.requireAuth)
  let { path } = to;
  let { requireAuth } = to.meta;
  // 判断是否有token
  let token = store.state.token;
  if (token) {
    // 有token且是login也则直接跳到首页
    if (path === '/login') {
      next({
        path: "/home"
      })
    } else {
      next(); // 继续往下操作
    }

  } else {
    // 无token,则判断是否需要页面权限，为true则打回登录页面，为false不需要权限则继续操作
    if (requireAuth) {
      // next({
      //     path: "/login"
      // })
      // 抑制掉重复重定向的问题
      router.push('/login').catch(() => { });
    } else {
      next();
    }
  }
})

router.afterEach((to, from) => {
  NProgress.done();
  // console.log('出去')
  // ...
})


// 暴露
export default router;
